﻿
var rapnet, isFancy;
function getParamVal(_rapnet) { 
    rapnet = Number($("#" + _rapnet).val()).toFixed(3);
    if (rapnet == "NaN" && i_rapnet != undefined)
        rapnet = i_rapnet;
}
//新增用
var i_rapnet
function getrapnet(param) {
    $.ajax({
        url: "/Team/GetRapnetByParam",//提交地址
        type: "Post",//提交方式 
        data:JSON.stringify(param),
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            result = JSON.parse(result);
            if (result.result) {
                i_rapnet = result.message;
            }
        }
    });
}

//公用计算方式
var DiaRate=Number($.cookie("DiasRate"));//销售汇率
var DiaPurchaseExchangerate=Number($.cookie("DiaPurchaseExchangerate"));//采购汇率
if(DiaPurchaseExchangerate=="" || DiaPurchaseExchangerate==null){
	DiaPurchaseExchangerate=DiaRate;
}
//_type为1计算原的，即汇率为销售汇率；2为计算采购的即为销售汇率
//退点框改变时计算退点，美金，人民币价格
function CalcAllByDiscountReturn(_sourceDiscountReturn, _sourceRmbGrain, _sourceDolarPerGrain, _sourceDolarPerCarat, _rate_, _rapnet, _weight,_type) {
	if(_type==1){
		var cal_rate=DiaRate;
	}else{
		var _rate=Number($("#"+_rate_).val());
		if(_rate=="" || _rate==null || _rate==undefined || _rate==0){
			var cal_rate=Number(DiaPurchaseExchangerate);
		}else{
			var cal_rate=_rate;
		}		
	}
    getParamVal(_rapnet);//得出rapnet
    var cal_weight=Number($("#" + _weight).val());//重量
    var cal_discount = 0;
    cal_discount = $('#' + _sourceDiscountReturn).val();//退点
    if (cal_discount <= 0) {
        cal_discount = 100 - Math.abs(cal_discount);
    }
    else {
        cal_discount = 100 + Math.abs(cal_discount);
    }
    var cal_price = (rapnet * (cal_discount)) / 100 *cal_weight;  //美金价格
    $('#' + _sourceRmbGrain).val(formatRMBPrice(cal_price * cal_rate));//人民币总价格
    $("#" + _sourceDolarPerGrain).val(formatUSDPrice(cal_price));   //usd 
    if (_sourceDolarPerCarat != ""){	
    	var per_price=formatUSDPrice(cal_price / cal_weight);
    	$('#' + _sourceDolarPerCarat).val(per_price);   //$/ct
    	if(_type==1){
    		$("#origin_rmb_per_price").val(formatRMBPrice(per_price*cal_rate));//￥/ct
    	}    	
    }       
}
//美金框改变时计算退点，美金，人民币价格
function CalcAllBySourceDolarPerGrain(_sourceDiscountReturn, _sourceRmbGrain, _sourceDolarPerGrain,_sourceDolarPerCarat, _rate_, _rapnet, _weight,_type) {
	if(_type==1){
		var cal_rate=DiaRate;
	}else{
		var _rate=Number($("#"+_rate_).val());
		if(_rate=="" || _rate==null || _rate==undefined){
			var cal_rate=DiaPurchaseExchangerate;
		}else{
			var cal_rate=_rate;
		}		
	}
	var cal_weight=Number($("#" + _weight).val());//重量
    getParamVal(_rapnet);
    var cal_price = Number($("#" + _sourceDolarPerGrain).val());//$
    var cal_discount = (cal_price*100) / (rapnet * cal_weight);
    var cal_return = cal_discount - 100;
    $("#" + _sourceDiscountReturn).val(cal_return.toFixed(3));//退点
    $('#' + _sourceRmbGrain).val(formatRMBPrice(cal_price * cal_rate));//￥
    if(_sourceDolarPerCarat!=""){
    	var per_price=formatUSDPrice(cal_price / cal_weight);
    	$('#' + _sourceDolarPerCarat).val(per_price);  //$/ct
    	if(_type==1){
    		$("#origin_rmb_per_price").val(formatRMBPrice(per_price*cal_rate));//￥/ct
    	}    	
    }
}
//美金单价改变 ，计算其他
function CalcAllByUSDPerPrice(_sourceDiscountReturn, _sourceRmbGrain, _sourceDolarPerGrain, _sourceDolarPerCarat, _rate_, _rapnet, _weight,_type){
	if(_type==1){
		var cal_rate=DiaRate;
	}else{
		var _rate=Number($("#"+_rate_).val());
		if(_rate=="" || _rate==null || _rate==undefined){
			var cal_rate=DiaPurchaseExchangerate;
		}else{
			var cal_rate=_rate;
		}		
	}
	getParamVal(_rapnet);
	var cal_weight=Number($("#" + _weight).val());//重量
	var per_price=Number($("#"+_sourceDolarPerCarat).val());//$/ct
	if(_type==1){
		$("#origin_rmb_per_price").val(formatRMBPrice(per_price*cal_rate));//￥/ct
	}
	var cal_price=formatUSDPrice(per_price*cal_weight);//$
	$("#"+_sourceDolarPerGrain).val(cal_price);//$
	$("#"+_sourceRmbGrain).val(formatRMBPrice(cal_price*cal_rate));//￥
	var cal_discount = (cal_price*100) / (rapnet * cal_weight);
    var cal_return = cal_discount - 100;
	$("#" + _sourceDiscountReturn).val(cal_return.toFixed(3));//退点
}
//人民币单价改变 ，计算其他
function CalcAllByRMBPerPrice(_sourceDiscountReturn, _sourceRmbGrain, _sourceDolarPerGrain, _sourceDolarPerCarat, _rate_, _rapnet, _weight,_type){
	if(_type==1){
		var cal_rate=DiaRate;
	}else{
		var _rate=Number($("#"+_rate_).val());
		if(_rate=="" || _rate==null || _rate==undefined){
			var cal_rate=DiaPurchaseExchangerate;
		}else{
			var cal_rate=_rate;
		}		
	}
	getParamVal(_rapnet);
	var cal_weight=Number($("#" + _weight).val());//重量
	var per_RMBprice=Number($("#origin_rmb_per_price").val());//￥/ct
	var per_price=formatUSDPrice(per_RMBprice/cal_rate);//$/ct
	$("#"+_sourceDolarPerCarat).val(per_price);//$/ct
	var cal_price=formatUSDPrice(per_price*cal_weight);//$
	$("#"+_sourceDolarPerGrain).val(cal_price);//$
	$("#"+_sourceRmbGrain).val(formatRMBPrice(cal_price*cal_rate));//￥
	var cal_discount = (cal_price*100) / (rapnet * cal_weight);
    var cal_return = cal_discount - 100;
	$("#" + _sourceDiscountReturn).val(cal_return.toFixed(3));//退点
}
//人民币框改变时计算退点，美金，人民币价格
function CalcAllBySourceRmbGrain(_sourceDiscountReturn, _sourceRmbGrain, _sourceDolarPerGrain, _sourceDolarPerCarat, _rate_, _rapnet, _weight,_type) {
	if(_type==1){
		var cal_rate=DiaRate;
	}else{
		var _rate=Number($("#"+_rate_).val());
		if(_rate=="" || _rate==null || _rate==undefined){
			var cal_rate=DiaPurchaseExchangerate;
		}else{
			var cal_rate=_rate;
		}		
	}
    getParamVal(_rapnet);
    var cal_weight=Number($("#" + _weight).val());//重量
    var cal_rmb_price = Number($("#" + _sourceRmbGrain).val());//￥
    var cal_price=formatUSDPrice(cal_rmb_price/cal_rate);//$
    var cal_discount = (cal_price*100) / (rapnet * cal_weight);
    var cal_return = cal_discount - 100;
    $("#"+_sourceDolarPerGrain).val(cal_price);//$
    $("#" + _sourceDiscountReturn).val(cal_return.toFixed(3));//退点
    if (_sourceDolarPerCarat != ""){
    	var per_price=formatUSDPrice(cal_price / cal_weight);
    	$('#' + _sourceDolarPerCarat).val(per_price);//$/ct
    	if(_type==1){
    		$("#origin_rmb_per_price").val(formatRMBPrice(per_price*cal_rate));//￥/ct
    	}    	
    }
       // $('#' + _sourceDolarPerCarat).val((parseFloat($("#" + _sourceDolarPerGrain).val()) / parseFloat($("#" + _weight).val())).toFixed(3));
}
//公用计算方式end 

//计算100、1
function calcPercent1Price(d, p) {
    var disc = (100 + d);
    var sp = p / disc;
    return sp;
}